/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "events_init.h"
#include "widgets_init.h"
#include "nrf_process.h"
#include "custom.h"


// 图标数量
#define icon_count          5
// 图标边缘间距（推荐20-30）
#define ICON_EDGE_GAP       20
// 小图标的尺寸
#define icon_size_small     70
// 大图标的尺寸
#define icon_size_big       120

// 定义图标结构体
typedef struct {
  lv_obj_t * obj;
  int32_t x;
} icon_typedef;

// 声明图标结构体数组
static icon_typedef icon[icon_count];


static lv_obj_t * imgs[icon_count];        // img arrary
static uint8_t imgsName[icon_count][20];  // Picture name arrary
// 当前焦点图片索引
static int current_index = icon_count/2;

// 当前选中图标索引
static int selected_idx = 0;


// 图标按下事件回调
static void icon_pressed_cb(lv_event_t * e)
{
    // 可添加按下时的行为
   uint8_t index = selected_idx;

    static uint8_t status_flags[4] = {0};
    nrf_para.nrf.start_send_flag = true;

    switch (index)
    {
        case 0:
            nrf_para.nrf.send_cmd = SNORING_POS_CMD;
            break;
        case 1:
            nrf_para.nrf.send_cmd = ZERO_G_POS_CMD;
            break;
        case 2:
            nrf_para.nrf.send_cmd = LEISUER_POS_CMD;
            break;
        case 3:
            nrf_para.nrf.send_cmd = FLAT_POS_CMD;
            break;
        case 4:
            nrf_para.nrf.send_cmd = SET_M1_POS_CMD;
            break;

        default:
            break;
    }

}

// 图标松开事件回调
static void icon_released_cb(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    printf("Icon Released: %s\n", lv_label_get_text(lv_obj_get_child(obj, 0)));
    // 可添加松手时的行为
}


// 图标切换函数
void scroll_motion2_icon_select(int dir, lv_ui *ui)
{
    // 保存之前选中的索引
    int prev_selected_idx = selected_idx;

    // 更新选中的图标索引
    if (dir == 0 && selected_idx > 0) selected_idx--;
    else if (dir == 1 && selected_idx < icon_count - 1) selected_idx++;
    else {
        if (!selected_idx) {
            selected_idx = icon_count - 1;
        } else {
            selected_idx = 0;
        }
    }

    // 获取当前选中图标的名称
    char *selected_icon_name = (char*)imgsName[selected_idx];
    if (lv_scr_act() == guider_ui.motion_1) {
        lv_label_set_text(ui->motion_1_label_1, selected_icon_name);
    } else if (lv_scr_act() == guider_ui.motion_2) {
        lv_label_set_text(guider_ui.motion_2_label_1, selected_icon_name);
    }

    // 更新所有图标的大小和可见性
    for (int i = 0; i < icon_count; i++) {
        if (i == selected_idx) {
            lv_obj_set_size(icon[i].obj, icon_size_big, icon_size_big);
            lv_obj_clear_flag(icon[i].obj, LV_OBJ_FLAG_HIDDEN);
        } else {
            lv_obj_set_size(icon[i].obj, icon_size_small, icon_size_small);
            lv_obj_add_flag(icon[i].obj, LV_OBJ_FLAG_HIDDEN);
        }
        lv_obj_align(icon[i].obj, LV_ALIGN_CENTER, 0, 0);
    }

    // 设置焦点到新选中的图标（修正函数调用）
    if (icon[selected_idx].obj) {
        lv_group_focus_obj(icon[selected_idx].obj); // LVGL 8.3只需要一个参数

        // 确保界面刷新
        lv_obj_invalidate(icon[selected_idx].obj);
        lv_obj_invalidate(icon[prev_selected_idx].obj);
    }
}


void setup_scr_motion_2(lv_ui *ui)
{
    lv_group_t * main_group = lv_group_create();//创建一个组
    lv_indev_set_group(indev_keypad, main_group);

    //Write codes motion_2
    ui->motion_2 = lv_obj_create(NULL);
    lv_obj_set_size(ui->motion_2, 240, 320);
    lv_obj_set_scrollbar_mode(ui->motion_2, LV_SCROLLBAR_MODE_OFF);

    //Write style for motion_2, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_bg_opa(ui->motion_2, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->motion_2, lv_color_hex(guider_ui.color.backlight), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->motion_2, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);

    //Write codes motion_2_mem_pos_img
    ui->motion_2_mem_pos_img = lv_img_create(ui->motion_2);
    lv_obj_add_flag(ui->motion_2_mem_pos_img, LV_OBJ_FLAG_CLICKABLE);
    lv_img_set_pivot(ui->motion_2_mem_pos_img, 50,50);
    lv_img_set_angle(ui->motion_2_mem_pos_img, 0);
    lv_obj_set_pos(ui->motion_2_mem_pos_img, 69, 109);
    lv_obj_set_size(ui->motion_2_mem_pos_img, 100, 100);

    //Write style for motion_2_mem_pos_img, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_img_recolor_opa(ui->motion_2_mem_pos_img, 138, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_img_recolor(ui->motion_2_mem_pos_img, lv_color_hex(0xfefefe), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_img_opa(ui->motion_2_mem_pos_img, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui->motion_2_mem_pos_img, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_clip_corner(ui->motion_2_mem_pos_img, true, LV_PART_MAIN|LV_STATE_DEFAULT);

    //Write codes motion_2_label_1
    ui->motion_2_label_1 = lv_label_create(ui->motion_2);
    lv_label_set_text(ui->motion_2_label_1, "ANTI SNDRE");
    lv_label_set_long_mode(ui->motion_2_label_1, LV_LABEL_LONG_WRAP);
    lv_obj_set_pos(ui->motion_2_label_1, 0, 280);
    lv_obj_set_size(ui->motion_2_label_1, 240, 32);

    //Write style for motion_2_label_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_border_width(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui->motion_2_label_1, lv_color_hex(0xffffff), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->motion_2_label_1, &lv_font_montserrat_18, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui->motion_2_label_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui->motion_2_label_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui->motion_2_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);

    //The custom code of motion_2.
    // 创建容器
    lv_obj_t *cont_row = lv_obj_create(ui->motion_2);
    lv_obj_set_size(cont_row, 240, 130);
    lv_obj_align(cont_row, LV_ALIGN_TOP_MID, 0, 85);
    lv_obj_set_scroll_dir(cont_row, LV_DIR_NONE);
    lv_obj_set_style_bg_opa(cont_row, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_opa(cont_row, LV_OPA_TRANSP, 0);
    lv_obj_set_scrollbar_mode(cont_row, LV_SCROLLBAR_MODE_OFF);

    // 初始化图标名称
    strcpy((char *)imgsName[0], "ANTI SNORE");
    strcpy((char *)imgsName[1], "ZERO GRANTY");
    strcpy((char *)imgsName[2], "LEISURE");
    strcpy((char *)imgsName[3], "FLAT");
    strcpy((char *)imgsName[4], "CUSTOM");

    for (uint8_t i = 0; i < icon_count; i++) {
        icon[i].obj = lv_obj_create(cont_row);
        icon[i].obj->user_data = &icon[i];

        // 设置基本样式
        lv_obj_set_style_bg_color(icon[i].obj, lv_color_hex(0x215682), LV_PART_MAIN);
        lv_obj_set_style_border_opa(icon[i].obj, LV_OPA_TRANSP, 0);
        lv_obj_set_scrollbar_mode(icon[i].obj, LV_SCROLLBAR_MODE_OFF);
        lv_obj_set_style_text_color(icon[i].obj, lv_color_white(), 0);

        // 设置焦点状态下的样式（纯边框效果，兼容所有LVGL版本）
        lv_obj_set_style_border_color(icon[i].obj, lv_color_hex(0x215682), LV_PART_MAIN | LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(icon[i].obj, 3, LV_PART_MAIN | LV_STATE_FOCUSED);
        lv_obj_set_style_border_opa(icon[i].obj, LV_OPA_COVER, LV_PART_MAIN | LV_STATE_FOCUSED);

        // 添加焦点状态下的背景颜色变化（可选）
        lv_obj_set_style_bg_color(icon[i].obj, lv_color_hex(0x316692), LV_PART_MAIN | LV_STATE_FOCUSED);

        lv_obj_t *label = lv_label_create(icon[i].obj);
        lv_label_set_text(label, (char*)imgsName[i]);
        lv_obj_center(label);

        // 设置事件回调
        lv_obj_add_event_cb(icon[i].obj, icon_pressed_cb, LV_EVENT_PRESSED, NULL);
        lv_obj_add_event_cb(icon[i].obj, icon_released_cb, LV_EVENT_RELEASED, NULL);

        // 默认初始化时，只有选中的图标为大，其他为小且隐藏
        if (i == selected_idx) {
            lv_obj_set_size(icon[i].obj, icon_size_big, icon_size_big);
            lv_obj_align(icon[i].obj, LV_ALIGN_CENTER, 0, 0);
            lv_obj_clear_flag(icon[i].obj, LV_OBJ_FLAG_HIDDEN);
            lv_label_set_text(ui->motion_2_label_1, (char*)imgsName[i]);
        } else {
            lv_obj_set_size(icon[i].obj, icon_size_small, icon_size_small);
            lv_obj_align(icon[i].obj, LV_ALIGN_CENTER, 0, 0);
            lv_obj_add_flag(icon[i].obj, LV_OBJ_FLAG_HIDDEN);
        }

        // 将图标对象添加到组中
        lv_group_add_obj(main_group, icon[i].obj);
    }

    // 设置初始焦点到选中的图标
    if (icon[selected_idx].obj) {
        lv_group_focus_obj(icon[selected_idx].obj);
    }

    // 初始化 imgs 映射数组
    for (uint8_t i = 0; i < icon_count; i++) {
        imgs[i] = icon[i].obj;
    }

    //Update current screen layout.
    lv_obj_update_layout(ui->motion_2);
}
